﻿
using Xacml.Core.Interfaces;
using Xacml.Core.Runtime;

namespace Xacml.Core.Runtime.Functions
{
  public abstract class BaseOneAndOnly : FunctionBase, ITypeSpecificFunction
  {
    public override IDataType Returns
    {
      get
      {
        return this.DataType;
      }
    }

    public override IDataType[] Arguments
    {
      get
      {
        return new IDataType[1]
        {
          (IDataType) DataTypeDescriptor.Bag
        };
      }
    }

    public abstract IDataType DataType { get; }

    protected object GetTypedValue(IFunctionParameter element)
    {
      return element.GetTypedValue(this.DataType, 0);
    }

    public override EvaluationValue Evaluate(EvaluationContext context, params IFunctionParameter[] args)
    {
      if (args[0].IsBag && args[0].BagSize == 1)
        return new EvaluationValue(this.GetTypedValue((IFunctionParameter) args[0].Elements[0]), this.DataType);
      return EvaluationValue.Indeterminate;
    }
  }
}
